package com.yc.jpyy.oadocitem.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSessionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.yc.jpyy.base.service.BaseService;
import com.yc.jpyy.base.util.PageBean;
import com.yc.jpyy.oadoc.dao.OADocDao;
import com.yc.jpyy.oadocitem.dao.OADocItemDao;
import com.yc.jpyy.oadocitem.service.OADocItemService;

@Service
public class OADocItemServiceImpl extends BaseService implements OADocItemService {

	@Autowired
	private OADocItemDao oaDocItemDao;
  
	/*
	  * 子表存入对应主表一对多的数据
	 * 
	 */
	@Override
	public int addOADocItemService(Map<String, Object> paramsMap) throws DataAccessException {

		String userIds = paramsMap.get("userids").toString();
		String[] userIdsArray = userIds.split(",");
		int arrayLength = userIdsArray.length;
		int sum = 0;
		int retValue = 1;
		int ret = 0;
		for (int i = 0; i < userIdsArray.length; i++) {
			paramsMap.put("userid", userIdsArray[i]);
			ret = oaDocItemDao.addOADocItemDao(paramsMap);
			sum = sum + ret;
		}
		if (sum != arrayLength) {
			retValue = 0;// 数据异常将返回值设置为0
			throw new SqlSessionException("数据库新增数据与数组接收数据不一致");
		}
	
	    //根据ParentID更新主表tb_OADocumentsCirculated的状态AuditStatus为3已下发、PublishTime下发布时间、PublishPeople下发人
		try {
		    int retNum = oaDocItemDao.updateOADocSimpColmnDao(paramsMap);
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
		return retValue;
	}

	@Override
	public int updateOADocItemService(Map<String, Object> paramsMap) throws DataAccessException {
		int ret = oaDocItemDao.updateOADocItemDao(paramsMap);
		return ret;

	}

	@Override
	public Map<String, Object> findOADocItemByIDServer(Map<String, Object> paramsMap) throws DataAccessException {
		return oaDocItemDao.findOADocItemByIdDao(paramsMap);
	}

	@Override
	public PageBean findOADocItemService(Map<String, Object> paramsMap) throws DataAccessException {
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum - 1) * pagesize);
		List list = oaDocItemDao.findOADocItemDao(paramsMap);
		PageBean pageBean = new PageBean();
		pageBean.setList(list);
		// 每页几条数据，默认20条
		pageBean.setPageSize(pagesize);
		// 得到数据库中的条数
		pageBean.setAllRow(this.findCountOADocItemService(paramsMap));
		// 当前第几页，默认第一页
		pageBean.setCurrentPage(pagenum);
		// 得到一共几页
		pageBean.setTotalPage(pageBean.getTotalPage());
		// 模仿百度分页，只显示10条数据
		pageBean.paginationTool(pagenum, pageBean.getTotalPage());
		return pageBean;
	}

	@Override
	public int findCountOADocItemService(Map<String, Object> paramsMap) throws DataAccessException {
		return oaDocItemDao.findCountOADocItemDao(paramsMap);
	}

	@Override
	public int delOADocItemService(Map<String, Object> InvestigInfo) throws DataAccessException {
		return oaDocItemDao.delOADocItemDao(InvestigInfo);
	}

	@Override
	public int updateOADocItemUserIDStatusService(Map<String, Object> paramsMap) throws DataAccessException {
		// TODO Auto-generated method stub
		return oaDocItemDao.updateOADocItemUserIDStatusDao(paramsMap);
	}

	@Override
	public int updateOADocItemStatusService(Map<String, Object> paramsMap) throws DataAccessException {
		int retResult = 0 ;
		//根据子表主键ID更新对应的状态
		int retValue =  oaDocItemDao.updateOADocItemStatusDao(paramsMap);
		if(retValue < 0){
			return retValue;
		}else{
			//查询出对应的父ID
			List<Integer> list = oaDocItemDao.findOADocItemParentIDDao(paramsMap);
	        Integer[] parentIdArray = list.toArray(new Integer[list.size()]);
	        for(int i = 0;i < parentIdArray.length;i++){
	        	paramsMap.put("ID", parentIdArray[0]);
	        	//查询主表对应的虽有子表中的状态是否已经完全更改为3
	        	int count = oaDocItemDao.findItemStatus(paramsMap);	
	        	if(count <= 0){
	        		//更新主表父ID对应的状态为4
			        retResult = oaDocItemDao.updateOADocsAuditStatusDao(paramsMap);
	        	}
	        }
		}
		return retResult;
	}

	@Override
	public int updateOADocItemStatusOnetoXService(Map<String, Object> paramsMap) throws DataAccessException {
		// TODO Auto-generated method stub
		return oaDocItemDao.updateOADocItemStatusOnetoXDao(paramsMap);
	}

	@Override
	public List<Map<String, Object>> findOADocItemByParentidService(Map<String, Object> paramsMap)
			throws DataAccessException {
		// TODO Auto-generated method stub
		return oaDocItemDao.findOADocItemByParentidDao(paramsMap);
	}

	@Override
	public Map<String, Object> updateOadocItemStatusById(
			Map<String, Object> paramsMap) throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		paramsMap.put("ids", paramsMap.get("ids").toString().split(","));
		int i = oaDocItemDao.updateOadocItemStatusById(paramsMap);
		if(i > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("returnMsg", "修改成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "修改失败");
		}
		return resultMap;
	}

}
